# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from zhipin.items import hupuItem

class HupuCrawlSpider(CrawlSpider):
    name = 'hupu_crawl'
    allowed_domains = ['bbs.hupu.com']
    start_urls = ['https://bbs.hupu.com/4860']

    rules = (
        Rule(LinkExtractor(allow=r'.+4860-[0-9]$'), callback='parse_item', follow=True),
    )

    def parse_item(self, response):
        item = hupuItem()
        titles = response.xpath('//*[@id="ajaxtable"]/div[1]/ul/li/div[1]/a/text()').extract()
        for title in titles:
            item['title'] = title
            yield item

    def parse_start_url(self,response):
        item = hupuItem()
        titles = response.xpath('//*[@id="ajaxtable"]/div[1]/ul/li/div[1]/a/text()').extract()
        for title in titles:
            item['title'] = title
            yield item